package org.forum.controller.interceptor;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DemographicStatistics implements HttpSessionListener,ServletRequestListener{
	
	Logger logger = LoggerFactory.getLogger(DemographicStatistics.class);
	
	private static int onlineN = 0;
	
	/**
	 * @pass_Test
	 * session的生成存在疑问
	 */
	@Override
	public void sessionCreated(HttpSessionEvent event) {
		onlineN++;
		event.getSession().setAttribute("onlineN", onlineN);
		if(logger.isDebugEnabled()){
			logger.debug(this.getClass() + "onlineN------>" + onlineN);
		}
	}

	/**
	 * @pass_Test
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		onlineN--;
		if(logger.isDebugEnabled()){
			logger.debug(this.getClass() + "onlineN------>" + onlineN);
		}
	}

	/**
	 * @pass_Test
	 * @return
	 */
	public static int getOnlineN() {
		return onlineN;
	}

	/**
	 * @pass_Test
	 */
	@Override
	public void requestDestroyed(ServletRequestEvent request) {
		if(logger.isDebugEnabled()){
			logger.debug("requestDestroyed...");
		}
	}

	/**
	 * @pass_Test
	 * 
	 */
	@Override
	public void requestInitialized(ServletRequestEvent event) {
		if(logger.isDebugEnabled()){
			logger.debug("requestInitialized...");
		}
	}
}
